﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using LayerDTO;

namespace LayerDAO
{
    public class HoaDonDAO
    {
        public List<HoaDonDTO> LayDanhSachHoaDon(string connect)
        {
            DataTable dt = new DataTable();
            DataProvider _provider = new DataProvider(connect);
            if (_provider.OpenConnect())
                dt = _provider.SqlExcuteQuery("LayDanhSachMatHang", null);
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            List<HoaDonDTO> list = new List<HoaDonDTO>();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    HoaDonDTO hoaDon = new HoaDonDTO();
                    hoaDon.MaHoaDon = dt.Rows[i]["MHD"].ToString();
                    hoaDon.MaKH = dt.Rows[i]["MKH"].ToString();
                    hoaDon.NgayLap = (DateTime)dt.Rows[i]["NgayLap"];
                   
                    hoaDon.DaXoa = (bool)dt.Rows[i]["DaXoa"];
                    list.Add(hoaDon);
                }
            }
            return list;
        }
        public string ThemHoaDon(HoaDonDTO hoaDon, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("ThemHoaDon", new SqlParameter("@NgayLap", hoaDon.NgayLap),
                                                             new SqlParameter("@maKH", hoaDon.MaKH), par);
                                                          

            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }


        public string XoaHoaDon(HoaDonDTO hoaDon, string connect)
        {

            DataProvider _provider = new DataProvider(connect);
            SqlParameter par = new SqlParameter("@return", SqlDbType.Int);
            par.Direction = ParameterDirection.ReturnValue;
            if (_provider.OpenConnect())
            {
                _provider.SqlExcuteScalar("XoaHoaDon", new SqlParameter("@maHD", hoaDon.MaHoaDon), par);


            }
            if (_provider.Error != "")
                throw new Exception(_provider.Error);
            return par.Value.ToString();
        }
    }
}